﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using NCS.IConnect.ApplicationContexts;

namespace JM.JCY.SharePointComponent.Department.DepartmentDocument
{
    public partial class DepartmentDocumentUserControl : UserControl
    {
        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
            }
            if (!IsPostBack)
            {
                if (Request.QueryString["DeptId"] != null)
                {
                    using (UserManagementProxy proxy = new UserManagementProxy())
                    {
                        var dt = proxy.GetDeptById(Int32.Parse(Request.QueryString["DeptId"].ToString()), string.Empty);

                        if (dt.Rows.Count > 0)
                        {
                            this.lblDeptNameDoc.Text = dt[0].DeptName;
                            //if (!IsMemberUser(_userId, Int32.Parse(Request.QueryString["DeptId"].ToString())))
                            //{
                            //    DivDeptDoc.Visible = false;
                            //}
                            //else
                            //{
                                DivDeptDoc.Visible = true;

                                // add list view
                                SPWeb myWeb = SPContext.Current.Web;
                                SPList docLibrary = myWeb.Lists[this.lblDeptNameDoc.Text];
                                    this.lvDeptDocument.ListId = docLibrary.ID.ToString();
                                    this.lvDeptDocument.ViewId = docLibrary.DefaultView.ID.ToString();
                                    string onclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", docLibrary.DefaultNewFormUrl);
                                    this.idHomePageNewItemDeptDoc.Attributes.Add("onclick", onclick);
                               
                        //}

                    }


                }

            }

        
            }
        }

        private bool IsExistList(SPWeb web, string listName)
        {
            bool isExist = false;
            SPListCollection lists = web.Lists;
            foreach (SPList item in lists)
            {
                if (item.Title.Equals(listName))
                {
                    isExist = true;
                    break;
                }

            }

            return isExist;
        }

        public bool IsMemberUser(int userId, int deptId)
        {
            try
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    var dt = proxy.GetUserDeptMatch(userId, deptId);
                    if (dt.Rows.Count > 0)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
